import { Controller, Post, Body, Response } from '@nestjs/common';
import { VerificationService } from './index.service';

type loginBodyType = {
  username: string;
  password: string;
};

type loginResType = {
  header: (key: string, value: any) => void;
  json: (param: { [key: string]: any }) => void;
};

@Controller('/verification')
export class VerificationController {
  constructor(private readonly verificationService: VerificationService) {}

  @Post('/login.do')
  doLogin(@Body() body: loginBodyType, @Response() res: loginResType) {
    if (body.username === 'admin' && body.password === '111111') {
      const token = this.verificationService.getToken();

      res.header('Set-Cookie', [
        'LOGINID=' + body.username + '; Path=/; HttpOnly',
        'LOGINTOKEN=' + token + '; Path=/; HttpOnly',
      ]);

      // 特别注意：使用@Response以后，需要主动处理返回
      res.json({
        code: '000000',
        msg: '登录成功',
      });
    } else {
      res.json({
        code: '999999',
        msg: '密码错误或用户不存在',
      });
    }
  }
}
